package com.morn.weibo.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.morn.weibo.exception.WeiboException;

public class Trends implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5195550918557777921L;

	private String name;

	private String query;

	private String url;

	public Trends(JSONObject json) throws WeiboException {
		init(json);
	}

	private void init(JSONObject json) throws WeiboException {
		if (json != null) {
			try {
				name = json.getString("name");
				query = json.getString("query");
				url = json.getString("url");
			} catch (JSONException e) {
				throw new WeiboException(
						e.getMessage() + ":" + json.toString(), e);
			}
		}

	}

	public static List<Trends> getTrendsList(JSONObject json)
			throws JSONException, WeiboException {
		if (json.has("trends")) {
			JSONArray array = json.getJSONArray("trends");
			int size = array.length();
			List<Trends> list = new ArrayList<Trends>();
			for (int i = 0; i < size; i++) {
				list.add(new Trends(array.getJSONObject(i)));
			}
			return list;
		}
		return null;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Trends [name=" + name + ", query=" + query + ", url=" + url
				+ "]";
	}

}
